package com.fduss.mazegame.client;

import com.fduss.mazegame.util.Array;

/**
 * This is the client message database, in all supported languages by
 * Language.
 */
// @TODO need to store the translation database in some files.
public class Localizer {

    // This is the translation database.
    private static final String GREETING_ENGLISH =
            System.lineSeparator() +
                    "                  ###########################               " + System.lineSeparator() +
                    "                  # Welcome to the MazeGame #               " + System.lineSeparator() +
                    "                  ###########################               " + System.lineSeparator() +
                    "                                                            " + System.lineSeparator() +
                    "               A tiny maze game demo for teaching Java.     " + System.lineSeparator() +
                    "                                                            " + System.lineSeparator() +
                    "                                                            " + System.lineSeparator() +
                    "Press the ENTER key to begin...";

    private static final String GREETING_CHINESE =
            System.lineSeparator() +
                    "                  #########################               " + System.lineSeparator() +
                    "                  #   欢迎来到迷宫游戏    #               " + System.lineSeparator() +
                    "                  #      ↖ ↗ ↘ ↙      #               " + System.lineSeparator() +
                    "                  #         ╯ ╰         #               " + System.lineSeparator() +
                    "                  #########################               " + System.lineSeparator() +
                    "                                                          " + System.lineSeparator() +
                    "             一个简单的用于java教学的迷宫游戏样例         " + System.lineSeparator() +
                    "                                                          " + System.lineSeparator() +
                    "                                                          " + System.lineSeparator() +
                    "按任意键开始游戏...";

    private static final String CONGRATULATIONS_ENGLISH =
            System.lineSeparator() +
                    "CONGRATULATIONS!" + System.lineSeparator() +
                    "YOU WIN!" + System.lineSeparator() +
                    System.lineSeparator();

    private static final String CONGRATULATIONS_CHINESE =
            System.lineSeparator() +
                    "恭喜！" + System.lineSeparator() +
                    "你赢了！" + System.lineSeparator() +
                    System.lineSeparator();

    private static final String ASK_FOR_COMMAND_ENGLISH =
            System.lineSeparator() +
                    "Write a command and press ENTER" + System.lineSeparator() +
                    "('h' for help): ";

    private static final String ASK_FOR_COMMAND_CHINESE =
            System.lineSeparator() +
                    "输入控制命令，点击回车" + System.lineSeparator() +
                    "('h + ENTER' 查看帮助信息): ";

    private static final String HELP_ENGLISH =
            System.lineSeparator() +
                    "Goal:" + System.lineSeparator() +
                    "\tYour goal is to take the player from" + System.lineSeparator() +
                    "\tthe starting point to the exit of the maze." + System.lineSeparator() +
                    System.lineSeparator() +
                    "Legend:" + System.lineSeparator() +
                    "\t%s   a wall" + System.lineSeparator() +
                    "\t%s   the hero" + System.lineSeparator() +
                    "\t%s   the exit of the maze" + System.lineSeparator() +
                    "\t%s   footprints" + System.lineSeparator() +
                    "\t%s   monsters" + System.lineSeparator() +
                    "\t%s   treasures" + System.lineSeparator() +
                    System.lineSeparator() +
                    "Commands:" + System.lineSeparator() +
                    "\th, help:   this help" + System.lineSeparator() +
                    "\tw, north:  go north" + System.lineSeparator() +
                    "\ts, south:  go south" + System.lineSeparator() +
                    "\td, east:   go east" + System.lineSeparator() +
                    "\ta, west:   go west" + System.lineSeparator() +
                    "\ta, kill:   kill the monster around you." + System.lineSeparator() +
                    "\tp, pick:   pick all the treasures around you" + System.lineSeparator() +
                    "\tc, check:  check your score" + System.lineSeparator() +
                    "\tb, back:   back step" + System.lineSeparator() +
                    "\tq, quit:   quit the game" + System.lineSeparator() +
                    "\tx, exit:   alias for \"quit\"" + System.lineSeparator() +
                    System.lineSeparator();

    private static final String HELP_CHINESE =
            System.lineSeparator() +
                    "游戏目标:" + System.lineSeparator() +
                    "\t你的目标是操纵人物" + System.lineSeparator() +
                    "\t从起点走到终点." + System.lineSeparator() +
                    System.lineSeparator() +
                    "符号说明:" + System.lineSeparator() +
                    "\t%s   墙" + System.lineSeparator() +
                    "\t%s   人物" + System.lineSeparator() +
                    "\t%s   终点" + System.lineSeparator() +
                    "\t%s   人物足迹" + System.lineSeparator() +
                    "\t%s   怪物" + System.lineSeparator() +
                    "\t%s   宝物" + System.lineSeparator() +
                    System.lineSeparator() +
                    "命令说明:" + System.lineSeparator() +
                    "\th, help:   这个帮助信息" + System.lineSeparator() +
                    "\tw, north:  向北走" + System.lineSeparator() +
                    "\ts, south:  向南走" + System.lineSeparator() +
                    "\td, east:   向东走" + System.lineSeparator() +
                    "\ta, west:   向西走" + System.lineSeparator() +
                    "\tk, kill:   杀死周围的怪物" + System.lineSeparator() +
                    "\tp, pick:   捡起周围的宝物" + System.lineSeparator() +
                    "\tc, check:  查看得分" + System.lineSeparator() +
                    "\tb, back:   返回上一步" + System.lineSeparator() +
                    "\tq, quit:   放弃游戏" + System.lineSeparator() +
                    "\tx, exit:   等同于quit" + System.lineSeparator() +
                    System.lineSeparator();

    private static final String EXITING_ENGLISH = System.lineSeparator() + "Exiting..." + System.lineSeparator()
            + System.lineSeparator();

    private static final String EXITING_CHINESE = System.lineSeparator() + "正在退出..." + System.lineSeparator()
            + System.lineSeparator();

    private static final String UNKNOWN_COMMAND_ENGLISH = System.lineSeparator() + "Unknown command: \"";

    private static final String UNKNOWN_COMMAND_CHINESE = System.lineSeparator() + "非法命令: \"";

    private static final String UNKNOWN_COMMAND_SUFIX_ENGLISH = "\"" + System.lineSeparator();

    private static final String UNKNOWN_COMMAND_SUFIX_CHINESE = "\"" + System.lineSeparator();

    private static final String MOVE_OK_ENGLISH = "OK";

    private static final String MOVE_OK_CHINESE = "好";

    private static final String MOVE_KO_ENGLISH = "Cannot move there!";

    private static final String MOVE_KO_CHINESE = "不能向那移动";

    private static final String UNDO_OK_ENGLISH = "OK";

    private static final String UNDO_OK_CHINESE = "好";

    private static final String UNDO_KO_ENGLISH = "No moves on the undo queue!";

    private static final String UNDO_KO_CHINESE = "没有可撤销的移动记录了!";

    private static final String HERO_DIED_ENGLISH = System.lineSeparator() + "You died." + System.lineSeparator();

    private static final String HERO_DIED_CHINESE = System.lineSeparator() + "你死了." + System.lineSeparator();

    private static final String KILL_MONSTER_OK_ENGLISH = System.lineSeparator() + "You killed a monster." + System.lineSeparator();

    private static final String KILL_MONSTER_OK_CHINESE = System.lineSeparator() + "你杀死了一个怪物" + System.lineSeparator();

    private static final String KILL_MONSTER_KO_ENGLISH = System.lineSeparator() + "there is no monster around you." + System.lineSeparator();

    private static final String KILL_MONSTER_KO_CHINESE = System.lineSeparator() + "周围没有怪物。" + System.lineSeparator();

    private static final String PICK_TREASURE_OK_ENGLISH = System.lineSeparator() + "You picked a treasure." + System.lineSeparator();

    private static final String PICK_TREASURE_OK_CHINESE = System.lineSeparator() + "你捡起了宝物。" + System.lineSeparator();

    private static final String PICK_TREASURE_KO_ENGLISH = System.lineSeparator() + "there is no treasure around you." + System.lineSeparator();

    private static final String PICK_TREASURE_KO_CHINESE = System.lineSeparator() + "周围没有宝物。" + System.lineSeparator();

    private static final String LEVEL_START_ENGLISH = System.lineSeparator()
            + "The level %d starts."
            + System.lineSeparator();

    private static final String LEVEL_START_CHINESE = System.lineSeparator()
            + "第%d开始"
            + System.lineSeparator();

    private static final String PASS_ALL_LEVEL_ENGLISH = System.lineSeparator()
            + "Congratulations!!! You have passed all levels."
            + System.lineSeparator();

    private static final String PASS_ALL_LEVEL_CHINESE = System.lineSeparator()
            + "恭喜你通过了所有关卡。"
            + System.lineSeparator();


    private static final String SCORE_ENGLISH = System.lineSeparator()
            + "You have moved %d steps" + System.lineSeparator()
            + "You have killed %d monsters" + System.lineSeparator()
            + "You have picked %d treasures" + System.lineSeparator();
    private static final String SCORE_CHINESE = System.lineSeparator()
            + "你移动了%d步" + System.lineSeparator()
            + "你杀死了%d个怪物" + System.lineSeparator()
            + "你捡了%d个宝物" + System.lineSeparator();

    private static final String[][] strings = {
            {
                    GREETING_ENGLISH,
                    CONGRATULATIONS_ENGLISH,
                    ASK_FOR_COMMAND_ENGLISH,
                    HELP_ENGLISH,
                    EXITING_ENGLISH,
                    UNKNOWN_COMMAND_ENGLISH,
                    UNKNOWN_COMMAND_SUFIX_ENGLISH,
                    MOVE_OK_ENGLISH,
                    MOVE_KO_ENGLISH,
                    UNDO_OK_ENGLISH,
                    UNDO_KO_ENGLISH,
                    HERO_DIED_ENGLISH,
                    KILL_MONSTER_OK_ENGLISH,
                    KILL_MONSTER_KO_ENGLISH,
                    PICK_TREASURE_OK_ENGLISH,
                    PICK_TREASURE_KO_ENGLISH,
                    SCORE_ENGLISH,
                    LEVEL_START_ENGLISH,
                    PASS_ALL_LEVEL_ENGLISH
            },
            {
                    GREETING_CHINESE,
                    CONGRATULATIONS_CHINESE,
                    ASK_FOR_COMMAND_CHINESE,
                    HELP_CHINESE,
                    EXITING_CHINESE,
                    UNKNOWN_COMMAND_CHINESE,
                    UNKNOWN_COMMAND_SUFIX_CHINESE,
                    MOVE_OK_CHINESE,
                    MOVE_KO_CHINESE,
                    UNDO_OK_CHINESE,
                    UNDO_KO_CHINESE,
                    HERO_DIED_CHINESE,
                    KILL_MONSTER_OK_CHINESE,
                    KILL_MONSTER_KO_CHINESE,
                    PICK_TREASURE_OK_CHINESE,
                    PICK_TREASURE_KO_CHINESE,
                    SCORE_CHINESE,
                    LEVEL_START_CHINESE,
                    PASS_ALL_LEVEL_CHINESE
            }
    };

    private Language lang;

    public Localizer(Language lang) {
        if (lang == null)
            throw new NullPointerException("lang");
        checkStringDB();
        this.lang = lang;
    }

    // Ugly hack to check the integrity of the static string
    // database at runtime.
    private void checkStringDB() {
        if (strings == null) {
            throw new NullPointerException("strings");
        }
        if (Array.hasNull(strings) ||
                !Array.isRect(strings)) {
            throw new IllegalStateException(
                    "The translation database is not a rectangular array");
        }
    }

    public String get(MsgToUsr msg) {
        return strings[lang.ordinal()][msg.ordinal()];
    }
}
